﻿@model IEnumerable<BussinessLogic.Models.Study.StudyPresentationModel>
@{
    ViewBag.Title = "Index";
}
@foreach (var item in Model)
{
    <div>
        <h2>@Html.DisplayFor(modelItem => item.Name)</h2>
        @Html.Raw(@System.Web.HttpUtility.HtmlDecode(item.Description))
        <ul>
            @foreach (var p in item.Prerequisites)
            {
                <li>
                    <h4>@Html.DisplayFor(modelItem => p.Name)</h4>
                      @Html.Raw(@System.Web.HttpUtility.HtmlDecode(item.Description))
                </li>
            }
        </ul>
        @if (item.Enrolled)
        {
            if (item.Approved)
            {
            <input type="button" name="Open" value="Take session" onclick="Open('@item.ID')"/>    
            }
            else 
            {
            <input type="button" name="Open" value="Take initial test" onclick="Initial('@item.ID')"/>
            }

        }
        else
        {
            <input type="button" name="Participate" value="Participate" onclick="Participate('@item.ID')"/>
        }
    </div>
    <hr />
}
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.19.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript">

    var InformedConsent = new Object();
    var studyId = 0;

    @foreach (var item in Model)
    {
        @: InformedConsent['@item.ID'] = ' @Html.Raw(@System.Web.HttpUtility.HtmlDecode(item.InformedConsent).Replace("'", "\\'"))';
    }

    var $consentDialog = $('<div></div>').dialog({
                                            autoOpen: false,
                                            modal: true,
			                                title: 'Informed Consent',
                                            resizable: false,
                                            height: 750,
                                            width: 850,
                                            buttons: {
				                                        Ok: function() {
                                                            // redirect to exercise
                                                            participateStudy(studyId);
                                                            Initial(studyId);
					                                        $(this).dialog( "close" );
				                                        },
				                                        Cancel: function() {
					                                        $( this ).dialog( "close" );
				                                        }
			                                        } 
                                          });

    function Open(id) {
    window.location = '@Url.Action("Daily", "Session")' + '/' +id;
    }

    function Initial(id) {
        window.location = '@Url.Action("Daily", "Session")' + '?id=' +id +'&initial=true';
    }

    function Participate(participateSudyId) {
        studyId = participateSudyId;
        $consentDialog.html(InformedConsent[participateSudyId]);
        $consentDialog.dialog('open');
    }

    function participateStudy(id) {
        $.post('@Url.Action("ParticipateStudy")', { 'id' : id}, function() {
                                                                    alert('participate successful to ' + id);
                                                                }
        );
    }

    $(document).ready(function() { 
        //alert('@Url.Action("Index", "Exercise")');
    });

</script>
